home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
108_01
/
datedemo.doc
< prev
next >
Wrap
Text File
|
1985-11-13
|
12KB
|
398 lines
.OP
SOME_USEFUL_C_TIME_FUNCTIONS
By Bill Bolton
Software Tools,
P.O. Box 80,
Newport Beach,
NSW, 2106
AUSTRALIA
Phone (02) 997-1018
.HESOME USEFUL C TIME FUNCTIONS, By Bill Bolton PAGE #
.PA
SOME_USEFUL_C_TIME_FUNCTIONS
ABSTRACT
Rather than show why you SHOULD use a programming language
(in this case C) this article endeavours to show why I HAVE used
it. An example program demonstrates some aspects of the language
and is directly useful to owners of the Mountain Hardware 100,000
day clock board as well as being generally useful to others with
real time clock facilities in their systems. A basic knowledge of
C will be required to understand the example program, it is
structured ,commented and brief extra descriptions of the
functions are given.
WHY_C?
While I have admired the C language from afar for a long
time it is only relatively recently that I have discovered its
true delights. Because PASCAL was supposed to be THE language for
structured programming and there was a need for more structure in
my programming efforts, I had given PASCAL a good try. The
system utilities which I was interested in writing just would not
come together properly under PASCAL, I had the distinct feeling
that the language was fighting me rather than helping my
programming efforts. My problems with PASCAL were not limited to
one implementation, PASCAL/M, PASCAL MT+, PASCAL/Z and UCSD
PASCAL all presented similar generic problems. Before the PASCAL
phreaks rise up again, let me stress that there are a lot of
things about PASCAL which seem very worthwhile, but I couldn't
get I to do what the things that I need done, in a way I could
grasp.
So for a time I retreated to assembler and FORTRAN for the
serious stuff and BASIC for the quickies. I found that it was
possible to write reasonably structured programs in BASIC-80 (1)
and FORTRAN 80 if a moderate amount of care was taken but
assembler was still the mainstay. A brief flirtation with FORTH
followed. FORTH showed great promise but the temptation to
produce code which even I couldn't understand a week later was
too great. Still, the idea of an extensible language is
fascinating !
In the meantime I had been reading "The C Programming
Language"(2) and it was clear that C was a language that was
structured but not to verbose or inflexible (my major problems
with PASCAL). It also appeared to have an extensible aspect in
that the overhead in writing, maintaining and using a library of
functions was quite small.
At about this time a major software project of the AWA Data
Group was started in C. I was able to look over the programmer's
shoulder as the project progressed and I liked what I saw. What's
more when the project finished the programmers involved still
seemed sane. I had a chance to see a working C compiler
(Whitesmith's for the PDP-11) in action and to study the
documentation. While the Whitesmith's compiler was out of my
financial range for starters, Leor Zolman had his BDS C compiler
available at a very reasonable price (on reflection I'd say its
an absolute steal) so that was my choice.
As soon as the compiler arrived I was thrown into the
situation that forced me to jump in at the deep end and use it
for a commercial project. I had quoted a client 4 hours to write
a file conversion program in assembler, it soon became obvious
that I had made a major error with my time prediction so I
decided to try and salvage the situation by writing the program
in C. This first programming effort in C went so smoothly that
the project was finished within the budgeted time and the client
got a product with error handling an order of magnitude better
than he would have received from an assembler program. Since then
I have abandoned BASIC almost entirely, only use FORTRAN for
maintenance and have greatly reduced the amount of assembler
programming. The description of C as "a PASCAL that's not afraid
to get its hands dirty"(3) is very, very apt.
If there is one single thing that delayed my entry in the
world of C it is probably the sparsity of references to C in the
microcomputer journals. There have been only a handful of
programs published and often they are examples that don't really
do anything useful (Small C in an obvious exception). Following
is one small effort to correct this situation.
THE_PROGRAM
DATEDEMO is a collection of C functions which will format a
string with the current date and current time fetched from a
Mountain Hardware 100,000 day clock S-100 board. Several date and
time formats are available by specification as one of the
arguments to the primary functions.
The date formats are those used by the nearly all "Western"
countries outside North America (this is an appropriate point to
complain loudly about software packages that lock the user into
MM/DD/YY date formats !!!) but are coded in a very general manner
so that nearly any imaginable date format could be easily
generated and extra formats are easily added. Time formats are
somewhat arbritary and only support 24 hour types (perhaps if
someone is keen they will do an AM/PM 12 hour type and contribute
it to DDJ).
In practice I use the functions to put date and time on in
the page headings of hardcopy output and in some business
packages. The functions live in my standard C function library
and are linked in at compile time. Normally the "#define" state
ments are loaded from a header file CLOCK.H but the are listed in
full here. While the program is written for compilation with the
BDS C compiler (Version 1.4X), it should also compile with the
other C compilers. The only non-standard library function is
'initw' which is the BDS kludge to allow array initialisation, as
BDS C does not support initialisers. As far as I know all other
library functions are supported by other C compilers (maybe not
Small C).
THE_FUNCTIONS
'main' - is simply an executable demonstration program to
tie the actual working functions together, all the
presently supported formats are displayed and a
typical use of the formatted strings is shown on
the last line displayed.
'date' - Attempts to get basic data to format a date string
form the clock, if this fails an error message is
displayed. Else the month and week name are
determined and the string formatted.
'get_date' - tests if the clock board is in the system. Then
collects digits from the clock to get the number of
days since 31/Dec/1977 (N.B. this day was a
Saturday, so a simple MOD 7 on the total number of
days since then makes Sunday day 1). This value is
then turned into the year, month and day of the
month.
'month_day'- Uses an initialised array to determine month and
day of the month from year and day of the year.
'ndays' - Returns number of days in the specified year.
'leap' - Thorough test for leap years.
'name_month'- Fills a string with a month name g